package com.example.check.service.serviceImpl;

import com.example.check.mapper.UserMapper;
import com.example.check.pojo.User;
import com.example.check.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }
    
    @Override
    public User getByUsername(String username) {
        return userMapper.selectByUsername(username);
    }
    
    @Override
    public User getByPhone(String phone) {
        return userMapper.selectByPhone(phone);
    }
    
    @Override
    public User getByEmail(String email) {
        return userMapper.selectByEmail(email);
    }
    
    @Override
    public User login(String usernameOrContact, String password) {
        String encodedPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        
        // 依次按用户名、手机号、邮箱尝试匹配，避免因为某些字段为空导致无法登录
        User user = userMapper.selectByUsername(usernameOrContact);
        if (user != null && encodedPwd.equals(user.getPassword())) {
            return user;
        }
        
        user = userMapper.selectByPhone(usernameOrContact);
        if (user != null && encodedPwd.equals(user.getPassword())) {
            return user;
        }
        
        user = userMapper.selectByEmail(usernameOrContact);
        if (user != null && encodedPwd.equals(user.getPassword())) {
            return user;
        }
        
        return null;
    }
    
    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (isUsernameExists(user.getUsername())) {
            return false;
        }
        
        // 检查手机号是否已存在
        if (user.getPhone() != null && isPhoneExists(user.getPhone())) {
            return false;
        }
        
        // 检查邮箱是否已存在
        if (user.getEmail() != null && isEmailExists(user.getEmail())) {
            return false;
        }
        
        // 设置默认值
        if (user.getName() == null || user.getName().trim().isEmpty()) {
            user.setName(user.getUsername()); // 如果name为空，使用username作为默认值
        }
        
        // 加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        
        return userMapper.insert(user) > 0;
    }
    
    @Override
    public boolean update(User user) {
        user.setUpdatedAt(LocalDateTime.now());
        return userMapper.update(user) > 0;
    }
    
    @Override
    public boolean deleteById(Integer id) {
        return userMapper.deleteById(id) > 0;
    }
    
    @Override
    public PageInfo<User> getPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectAll();
        return new PageInfo<>(users);
    }
    
    @Override
    public List<User> getAll() {
        return userMapper.selectAll();
    }
    
    @Override
    public boolean isUsernameExists(String username) {
        return userMapper.selectByUsername(username) != null;
    }
    
    @Override
    public boolean isPhoneExists(String phone) {
        return userMapper.selectByPhone(phone) != null;
    }
    
    @Override
    public boolean isEmailExists(String email) {
        return userMapper.selectByEmail(email) != null;
    }
}
